/**
 * Created by maiseries on 2018/1/2.
 */
import Request from '../utils/Request'

export default class AssetService {

    static AUDIT_STATE = {
        EDIT: '01',
        APPROVING_FIRST: '02',
        APPROVING_SECOND: '03',
        REJECT_FIRST: '04',
        PASSED: '05',
        REJECT_SECOND: '06',
        SUCCEED: '07',
        REFUSED_FIRST: '08',
        REFUSED_SECOND: '09',
    };

    static LOGS_STATE = {
        REQUIREMENT_COMMIT: "01", //资产管理人提交需求

        // AUTHENTICATION_COMMIT: "02", //资产管理人提交认证
        // AUTHENTICATION_REJECT: "03", //操作人认证驳回
        // AUTHENTICATION_PASS: "04", //操作人认证通过

        // REQUIREMENT_REJECT: "05", //操作人驳回需求
        // REQUIREMENT_REFUSE: "06", //操作人拒绝需求
        // REQUIREMENT_SUPPLY: "07", //资产管理人补充附件
        REQUIREMENT_PASS: "08", //操作人通过需求并初始化资产

        ASSET_SUPPLY: "09", //资产管理人补充资产
        ASSET_FIRST_REJECT: "10", //操作人驳回资产
        ASSET_FIRST_UPDATE: "11", //操作人修改资产
        ASSET_FIRST_REFUSE: "12", //操作人拒绝资产
        ASSET_FIRST_PASS: "13", //操作人通过资产

        ASSET_SECOND_REJECT: "14", //审核人驳回资产
        ASSET_SECOND_UPDATE: "15", //审核人修改资产
        ASSET_SECOND_REFUSE: "16", //审核人拒绝资产

        ASSET_SECOND_PASS: "17", //审核人通过资产并填写服务费率及上传合同附件
        ASSET_FINISHED: "18", //操作人修改资产状态为服务完成
    };

    static async list(options) {
        options.url = '/asset/list';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async detail(options) {
        options.url = '/asset/detail';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            data.progress = this.formatProgress(data);
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static formatProgress(data) {
        const form = data.detail;
        const logs = data.businessLogs || [];

        const res = [
            {
                title: '补充资产资料',
                date: logs.find(value => value.detail_type === this.LOGS_STATE.ASSET_SUPPLY) ? logs.find(value => value.detail_type === this.LOGS_STATE.ASSET_SUPPLY).create_date : '',
                description: form.audit_status === this.AUDIT_STATE.REJECT_FIRST || form.audit_status === this.AUDIT_STATE.REJECT_SECOND || form.audit_status === this.AUDIT_STATE.EDIT ? '' : '资料提交成功',
                active: form.audit_status === this.AUDIT_STATE.REJECT_FIRST || form.audit_status === this.AUDIT_STATE.REJECT_SECOND || form.audit_status === this.AUDIT_STATE.EDIT
            },{
                title: '审核资产',
                date: logs.find(value => value.step === '06') ? logs.find(value => value.step === '06').create_date : '',
                description: form.audit_status === this.AUDIT_STATE.REJECT_FIRST || form.audit_status === this.AUDIT_STATE.REJECT_SECOND || form.audit_status === this.AUDIT_STATE.REFUSED_FIRST || form.audit_status === this.AUDIT_STATE.REFUSED_SECOND ? form.audit_reason : '',
                type: 'error',
                active: form.audit_status === this.AUDIT_STATE.APPROVING_FIRST || form.audit_status === this.AUDIT_STATE.APPROVING_SECOND
            }
        ];

        if (form.audit_status === this.AUDIT_STATE.REFUSED_SECOND || form.audit_status === this.AUDIT_STATE.REFUSED_FIRST) {
            res.push({
                title: '审核结果',
                date: form.audit_date,
                description: '审核已驳回',
                active: true
            });
        } else {
            res.push({
                title: '协议签署中',
                date: logs.find(value => value.detail_type === this.LOGS_STATE.ASSET_SECOND_PASS) ? logs.find(value => value.detail_type === this.LOGS_STATE.ASSET_SECOND_PASS).create_date : '',
                description: '',
                active: form.audit_status === this.AUDIT_STATE.PASSED
            });
            res.push({
                title: '审核结果',
                date: logs.find(value => value.detail_type === this.LOGS_STATE.ASSET_FINISHED) ? logs.find(value => value.detail_type === this.LOGS_STATE.ASSET_FINISHED).create_date : '',
                description: form.audit_status === this.AUDIT_STATE.SUCCEED ? '服务已完成' : '',
                active: form.audit_status === this.AUDIT_STATE.SUCCEED
            });
        }

        return res;
    }

}